package com.hdculture.simple.expensemanager.premium;

import java.text.DecimalFormat;
import java.util.Calendar;

import com.hdculture.simple.expensemanager.premium.R;

import android.app.Activity;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.util.Log;
import android.view.*;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;

public class ManageExpenseActivity extends Activity {
    /** Called when the activity is first created. */
	Button backButton;
	CoreDBAdapter dbAdapter;
	TextView thisMonthText, lastMonthText, ERiseFall, thisMonthNameText, lastMonthNameText;
	ListView choiceList;
	String currencySign="$";
	String[] choices = {"Expense by Month","Expense by Item","All Expense"};
	String tag = "ManageExpenseActivity";
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
    	dbAdapter.close();
    	Log.i(tag,"DB Connection Closed");
		super.onPause();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		dbAdapter.open();
		Log.i(tag,"DB Connection Resumed");
		super.onResume();
	}
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manageexpense);
        dbAdapter = new CoreDBAdapter(ManageExpenseActivity.this);
        dbAdapter.open();
        Log.i(tag,"DB Connection Open");
        currencySign = dbAdapter.getCurrencySign();
        choiceList = (ListView)findViewById(R.id.choiceList);
        choiceList.setAdapter(new ArrayAdapter<String>(ManageExpenseActivity.this,android.R.layout.simple_list_item_1,choices));
        backButton = (Button)findViewById(R.id.backButton);
        backButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
        
        thisMonthText = (TextView)findViewById(R.id.thisMonthExpenseText);
        thisMonthNameText = (TextView)findViewById(R.id.thisMonthNameText);
        lastMonthText = (TextView)findViewById(R.id.lastMonthExpenseText);
        lastMonthNameText = (TextView)findViewById(R.id.lastMonthNameText);
        ERiseFall = (TextView)findViewById(R.id.ERiseFall);
        updateTotalExpense();
        
        choiceList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch(arg2){
				case 0:	//By Month
						Intent monthSelector = new Intent(ManageExpenseActivity.this,MonthSelector.class);
						monthSelector.putExtra("ISINCOME", false);
						startActivity(monthSelector);
						break;
				case 1: //By Item
						Intent itemSelector = new Intent(ManageExpenseActivity.this,ManageExpenseActivity_Items.class);
						startActivity(itemSelector);
						break;
				case 2:	//All Expense
						Intent i = new Intent(ManageExpenseActivity.this,AllExpenseActivity.class);
						startActivity(i);
						break;
				}
			}
		});
    }
    
    public void updateTotalExpense(){
    	double thisMonthExpenseDbl=0.00;
    	double lastMonthExpenseDbl=0.00;
    	int thisMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
    	int lastMonth = thisMonth-1;
    	if(lastMonth==0){
    		lastMonth=12;
    	}
    	thisMonthNameText.setText(getMonthName(thisMonth)+":");
    	lastMonthNameText.setText(getMonthName(lastMonth)+":");
    	Cursor c = dbAdapter.fetchTotalExpenseThisMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
    	if(c.isAfterLast()==false){
    		String incomeString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,##0.00");
    		thisMonthExpenseDbl = c.getDouble(0);
    		incomeString = currencyFormat.format(thisMonthExpenseDbl);
    		thisMonthText.setText(incomeString);
    	}
    	c = dbAdapter.fetchTotalExpenseLastMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
		if(c.isAfterLast()==false){
    		String expenseString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,##0.00");
    		lastMonthExpenseDbl=c.getDouble(0);
    		expenseString = currencyFormat.format(lastMonthExpenseDbl);
    		lastMonthText.setText(expenseString);
    	}
	
    	double iPercent=0.00;
    	try{
    		iPercent = ((thisMonthExpenseDbl-lastMonthExpenseDbl)/lastMonthExpenseDbl)*100;
    	}catch(Exception ex){
    		iPercent=0.00;
    	}
    	DecimalFormat percentDigit = new DecimalFormat("#0.0");
    	ERiseFall.setText("  "+percentDigit.format(iPercent)+"%");
    	if(iPercent<0){
    		ERiseFall.setTextColor(Color.GREEN);
    	}else if(iPercent>5){
    		ERiseFall.setTextColor(Color.RED);
    	}
    }
    
    public String getMonthName(int month){
    	String monthString = "";
        switch(month){
        case 1:	monthString = "January";break;
        case 2: monthString = "February";break;
        case 3: monthString = "March";break;
        case 4: monthString = "April";break;
        case 5: monthString = "May";break;
        case 6: monthString = "June";break;
        case 7: monthString = "July";break;
        case 8: monthString = "August";break;
        case 9: monthString = "September";break;
        case 10: monthString = "October";break;
        case 11: monthString = "November";break;
        case 12: monthString = "December";break;
        }
        return monthString;
    }
}